
#include "CheckerMaterial.h"
#include "HitRecord.h"
#include "Primitive.h"
#include "Ray.h"
#include "Scene.h"
#include "Math.h"
using namespace std;

CheckerMaterial::CheckerMaterial(const Point& origin, const Vector& v1, const Vector& v2,
                                 Material* matl0, Material* matl1)
  : origin(origin), v1(v1), v2(v2), v3(0,0,0)
{
  this->v1 *= 1./v1.length2();
  this->v2 *= 1./v2.length2();
  matls[0] = matl1;
  matls[1] = matl0;
  matls[2] = matl1;
}

CheckerMaterial::CheckerMaterial(const Point& origin,
                                 const Vector& v1, const Vector& v2, const Vector& v3,
                                 Material* matl0, Material* matl1)
  : origin(origin), v1(v1), v2(v2), v3(v3)
{
  this->v1 *= 1./v1.length2();
  this->v2 *= 1./v2.length2();
  this->v3 *= 1./v3.length2();
  matls[0] = matl0;
  matls[1] = matl1;
  matls[2] = matl0;
}

CheckerMaterial::~CheckerMaterial()
{
}

void CheckerMaterial::shade(Color& result, const RenderContext& context,
                            const Ray& ray, const HitRecord& hit,
                            const Color& atten, int depth) const
{
  Vector uvw;
  hit.getPrimitive()->computeUVW(uvw, context, ray, hit);
  int i1 = Floor(Dot(uvw, v1));
  int i2 = Floor(Dot(uvw, v2));
  int i3 = Floor(Dot(uvw, v3));
  int which_matl = (i1+i2+i3)%2+1;
  matls[which_matl]->shade(result, context, ray, hit, atten, depth);
}